package com.skit.main.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.skit.common.ResourcesManager
import com.skit.common.bean.ActivityBean
import com.skit.main.R
import kotlinx.android.synthetic.main.main_re_item_activity_layout.view.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author shuike [Contact me.](mailto:shuike007@gmail.com)
 * @version 1.0
 * @since 2020/5/19
 */

class ActivityAdapter(private val list: List<ActivityBean>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val format = SimpleDateFormat("yyyy.MM.dd", Locale.getDefault())

    var onItemClickListener: ((View, Int) -> Unit?)? = null

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.image
        val title: TextView = itemView.title
        val activity_start_time: TextView = itemView.activity_start_time
        val apply_time: TextView = itemView.apply_time
        val event_location: TextView = itemView.event_location
        val sponsorIcon: ImageView = itemView.sponsor_icon
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context)
                .inflate(R.layout.main_re_item_activity_layout, parent, false)
        )

//        inflate.number_of_apply.visibility = View.GONE
//        return when (viewType) {
//            Type.ACTIVITY.ordinal -> ViewHolder(
//                LayoutInflater.from(parent.context)
//                    .inflate(R.layout.main_re_item_activity_layout, parent, false)
//            )
//
//            Type.TYPE.ordinal -> TypeHolder(
//                LayoutInflater.from(parent.context)
//                    .inflate(R.layout.recycler_type_item, parent, false)
//            )
//
//            else -> ViewHolder(
//                LayoutInflater.from(parent.context)
//                    .inflate(R.layout.main_re_item_activity_layout, parent, false)
//            )
//        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ViewHolder) {
            viewHolder(holder, position)
        }
    }

    private fun viewHolder(holder: ViewHolder, position: Int) {

        val bean = list[position]
        Glide.with(holder.itemView).load(bean.activityImages[0])
            .into(holder.imageView)
        holder.title.text = bean.activityName
        holder.event_location.text = ResourcesManager.getRes().getString(
            R.string.main_event_location, bean.eventLocation
        )

        holder.activity_start_time.text = ResourcesManager.getRes().getString(
            R.string.activity_time,
            format.format(bean.startTime),
            format.format(bean.endTime)
        )

        holder.apply_time.text = ResourcesManager.getRes().getString(
            R.string.apply_time,
            format.format(bean.signUpStartTime),
            format.format(bean.signUpEndTime)
        )

        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(it, position)
        }

        holder.itemView.number_of_apply.text =
            when {
                bean.startTime > System.currentTimeMillis() -> {
                    "待开始"
                }
                bean.endTime < System.currentTimeMillis() -> "已结束"
                else -> "已开始"
            }
        holder.itemView.sponsor.text = bean.organizer?.departmentName
//        val drawable = holder.sponsorIcon.drawable
//        val drawable = ContextCompat.getDrawable(holder.itemView.context, R.drawable.ic_baseline_school_24)
//        DrawableCompat.setTint(drawable, Color.BLUE)
//        holder.sponsorIcon.setImageDrawable(drawable)

        holder.itemView.number_of_apply_2.text = ResourcesManager.getRes().getString(
            R.string.person_apply, bean.registeredNum
        )
        val layoutParams =
            holder.itemView.number_of_apply.layoutParams as RelativeLayout.LayoutParams
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP)
    }

    override fun getItemCount(): Int = list.size
}